import fs from 'fs-extra'
import path from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const rootDir = path.resolve(__dirname, '..')
const electronDir = path.join(rootDir, 'electron')
const distElectronDir = path.join(rootDir, 'dist-electron')

// 确保目标目录存在
fs.ensureDirSync(distElectronDir)

// 复制 electron 文件
const files = ['main.cjs', 'preload.cjs']

files.forEach(file => {
  const src = path.join(electronDir, file)
  const dest = path.join(distElectronDir, file.replace('.cjs', '.js'))
  
  if (fs.existsSync(src)) {
    fs.copyFileSync(src, dest)
    console.log(`✓ Copied ${file} -> ${path.basename(dest)}`)
  } else {
    console.error(`✗ File not found: ${file}`)
  }
})

// 在 dist-electron 目录创建 package.json，指定使用 CommonJS
const distPackageJson = {
  type: 'commonjs'
}

fs.writeJsonSync(
  path.join(distElectronDir, 'package.json'),
  distPackageJson,
  { spaces: 2 }
)

console.log('✓ Created dist-electron/package.json (type: commonjs)')
console.log('Electron files copied successfully!')

